package com.nulldev.util.graphics.renderIt.api.core;

import static com.nulldev.util.graphics.renderIt.api.renderItConstants.*;

import java.awt.Component;
import java.awt.Rectangle;

import javax.swing.JComponent;

import com.nulldev.util.VariableAPI.MathUtil;

public interface IRenderCallbackMetadata {
	public static class IRenderCallbackMetadataStub implements IRenderCallbackMetadata {

		@Override
		public String toString() {
			return "IRenderCallbackMetadata[hash=" + MathUtil.toHex(super.hashCode()) + ",x=" + RIT_NOT_SUPPORTED + ",y=" + RIT_NOT_SUPPORTED + ",width="
					+ RIT_NOT_SUPPORTED + ",height=" + RIT_NOT_SUPPORTED + ",type=" + RIT_PAINT_REQUEST_TYPE_STANDARD + "]";
		}
	}

	public static class IRenderCallbackMetadataVB implements IRenderCallbackMetadata {

		private final JComponent swing_comp;
		private final Component awt_comp;

		private Rectangle r;

		public IRenderCallbackMetadataVB(final Component comp) {
			this.awt_comp = comp;
			this.swing_comp = null;
		}

		public IRenderCallbackMetadataVB(final JComponent comp) {
			this.swing_comp = comp;
			this.awt_comp = null;
		}

		public void updateBounds() {
			if (this.awt_comp != null) {
				this.r = this.awt_comp.getBounds();
			} else {
				this.r = this.swing_comp.getVisibleRect();
			}
		}

		@Override
		public int getX() {
			if (this.r == null)
				return RIT_NOT_SUPPORTED;
			return (int) this.r.getX();
		}

		@Override
		public int getY() {
			if (this.r == null)
				return RIT_NOT_SUPPORTED;
			return (int) this.r.getY();
		}

		@Override
		public int getWidth() {
			if (this.r == null)
				return RIT_NOT_SUPPORTED;
			return this.r.width;
		}

		@Override
		public int getHeight() {
			if (this.r == null)
				return RIT_NOT_SUPPORTED;
			return this.r.height;
		}

		@Override
		public int getPaintRequestType() {
			if (this.r == null)
				return RIT_PAINT_REQUEST_TYPE_STANDARD;
			return RIT_PAINT_REQUEST_TYPE_REPAINT_SELECTIVE;
		}

		@Override
		public String toString() {
			return "IRenderCallbackMetadata[hash=" + MathUtil.toHex(super.hashCode()) + ",x=" + r.x + ",y=" + r.y + ",width=" + r.width + ",height=" + r.height
					+ ",type=" + RIT_PAINT_REQUEST_TYPE_REPAINT_SELECTIVE + "]";
		}

	}

	public static IRenderCallbackMetadata makeFromVisibleBounds(final JComponent comp) {
		return new IRenderCallbackMetadataVB(comp);
	}

	public static final IRenderCallbackMetadata DEFAULT = new IRenderCallbackMetadataStub();

	public default int getX() {
		return RIT_NOT_SUPPORTED;
	}

	public default int getY() {
		return RIT_NOT_SUPPORTED;
	}

	public default int getWidth() {
		return RIT_NOT_SUPPORTED;
	}

	public default int getHeight() {
		return RIT_NOT_SUPPORTED;
	}

	public default int getPaintRequestType() {
		return RIT_PAINT_REQUEST_TYPE_STANDARD;
	}
}
